package com.erp.module.duizhangPhoto.XxDuizhangPhoto

import android.app.Activity
import android.content.Intent
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.activity.DanjianivActivity
import com.erp.bean.DuizhangBean
import com.erp.bean.PhotoBean
import com.erp.module.duizhangPhoto.DuizhangPhotoContract
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.GvphotoAdapter
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_duizhang_photo.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.*
import kotlin.collections.ArrayList

class XxDuizhangPhotoActivity: BaseActivity(),DuizhangPhotoContract.XxDuizhangPhotoView, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, View.OnClickListener {


    lateinit var duizhangbean: DuizhangBean
    lateinit var deletephoto: MutableList<PhotoBean>
    lateinit var duizhangphotoAdapter: GvphotoAdapter
    lateinit var IXP: IXxDuizhangPhoto
    lateinit var updatedialog: AlertDialog
    lateinit var id: String
    var code: Int = 0
    override fun getLayoutId(): Int {
        return R.layout.activity_duizhang_photo
    }

    override fun initCreate() {
        duizhangbean = DuizhangBean()
        deletephoto = ArrayList<PhotoBean>()
        code = intent.getIntExtra("code", 0)
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        if(code == 2){
            tv_header_title.text = "上传对账照片"
        }else if(code == 1){
            id = intent.getStringExtra("id")
            tv_header_title.text = "查看对账照片"
        }
        if(code == 1){
            TvYear.visibility = View.VISIBLE
            TvMonth.visibility = View.VISIBLE
            AtYear.visibility = View.GONE
            SpMonth.visibility = View.GONE
        }
        if(code == 2){
            TvYear.visibility = View.GONE
            TvMonth.visibility = View.GONE
            AtYear.visibility = View.VISIBLE
            SpMonth.visibility = View.VISIBLE
        }
        var listyear = shengcengshuzu(2000, 2050)
        var listmonth = shengcengshuzu(1, 12)
        val avyaer = ArrayAdapter(this@XxDuizhangPhotoActivity, R.layout.simple_at_item, listyear)
        AtYear.setAdapter(avyaer)
        AtYear.setText( Calendar.getInstance().get(Calendar.YEAR).toString())

        val adaptertime = ArrayAdapter(this@XxDuizhangPhotoActivity, R.layout.simple_at_item, listmonth)
        SpMonth.adapter = adaptertime
        SpMonth.setSelection(0)

        BtQueding.setOnClickListener(this)
        duizhangphotoAdapter = GvphotoAdapter(duizhangbean.listphoto, this)
        duizhangphotoAdapter.isadd(true)
        GvDuizhangPhoto.onItemClickListener = this
        GvDuizhangPhoto.adapter = duizhangphotoAdapter
        GvDuizhangPhoto.onItemLongClickListener = this



        IXP = IXxDuizhangPhoto(this, this, duizhangbean, code)
        if(code == 1){
            updatedialog = Myutil.showupdatedialog(this@XxDuizhangPhotoActivity, "正在获取对账单照片", false)
            IXP.getDuizhangPhoto(id)
        }
    }

    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        if(duizhangbean.isShenhe){
            return true
        }
        val dialog = AlertDialog.Builder(this@XxDuizhangPhotoActivity)
        dialog.setTitle("提示")
        dialog.setMessage("确定要删除这个照片")
        dialog.setPositiveButton("确定") { dialog, which ->
            deletephoto.add(duizhangbean.listphoto[position])
            duizhangbean.listphoto.remove(duizhangbean.listphoto[position])
            duizhangphotoAdapter.notifyDataSetChanged()
        }
        dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
        dialog.show()
        return true
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        if (position == duizhangbean.listphoto.size) {
            Dialogutil.zhiphoto(this@XxDuizhangPhotoActivity, null)
        } else {
            val intent = Intent(this@XxDuizhangPhotoActivity, DanjianivActivity::class.java)
            intent.putExtra("url", duizhangbean.listphoto[position].uri)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                99 -> {
                    val selectedImage = data.data
                    val filePathColumns = arrayOf(MediaStore.Images.Media.DATA)
                    if(selectedImage != null){
                        val c = contentResolver.query(selectedImage, filePathColumns, null, null, null)
                        c!!.moveToFirst()
                        val columnIndex = c.getColumnIndex(filePathColumns[0])
                        val imagePath = c.getString(columnIndex)
                        addphoto(imagePath)
                    }

                }
                100 -> {
                    val name = data.getStringExtra("dir")
                    addphoto(name)
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            BtQueding -> {
                if(code == 2){
                    duizhangbean.year = AtYear.text.toString()
                    duizhangbean.month = SpMonth.selectedItem.toString()
                }
                updatedialog = Myutil.showupdatedialog(this@XxDuizhangPhotoActivity, "正在上传对账照片", false)
                IXP.addDuizhangPhoto(deletephoto)
            }
        }
    }

    private fun addphoto(name: String) {
        val photo = PhotoBean()
        photo.uri = name
        duizhangbean.listphoto.add(photo)
        duizhangphotoAdapter.notifyDataSetChanged()
    }

    override fun getDuizhangPhotoOk() {
        Log.d("addDuizhangPhotoOk",duizhangbean.toString())
        TvYear.visibility = View.VISIBLE
        TvMonth.visibility = View.VISIBLE
        AtYear.visibility = View.GONE
        SpMonth.visibility = View.GONE
        Myutil.settv(duizhangbean.year,TvYear)
        Myutil.settv(duizhangbean.month,TvMonth)
        if(duizhangbean.audit_time != ""){
            TvAuditTime.visibility = View.VISIBLE
            BtQueding.visibility = View.GONE

        }else{
            TvAuditTime.visibility = View.GONE
            BtQueding.visibility = View.VISIBLE
        }
        Myutil.settv(Myutil.getdata(duizhangbean.audit_time),TvAuditTime)
//        AtYear.setText(duizhangbean.year)
//        SpMonth.setText(duizhangbean.month)
        duizhangphotoAdapter.notifyA(duizhangbean.listphoto)
        updatedialog.dismiss()
    }

    override fun getDuizhangPhotoNo() {
        updatedialog.dismiss()
    }

    override fun addDuizhangPhotoOk() {
        Log.d("addDuizhangPhotoOk",duizhangbean.toString())
        Myutil.MyTost("上传成功")
        val intent = Intent()
        setResult(Activity.RESULT_OK, intent)
        finish()
//        TvYear.visibility = View.VISIBLE
//        TvMonth.visibility = View.VISIBLE
//        AtYear.visibility = View.GONE
//        SpMonth.visibility = View.GONE
//        Myutil.settv(duizhangbean.year,TvYear)
//        Myutil.settv(duizhangbean.month,TvMonth)
//        if(duizhangbean.isShenhe){
//            BtQueding.visibility = View.GONE
//        }
//        duizhangphotoAdapter.notifyA(duizhangbean.listphoto)
//        updatedialog.dismiss()
    }

    override fun addDuizhangPhotoNo() {
        updatedialog.dismiss()
    }

    fun shengcengshuzu(q:Int, z:Int):List<Int>{
        val list = ArrayList<Int>()
        var q1 = q
        while(q1 < z){
            list.add(q1)
            q1++
        }
        return list
    }

    fun getxiabiao(q:Int,list:List<Int>):Int{
        for (i in list.indices) {
            if(q == list[i]){
                return q
            }
        }
        return 0
    }

}